﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace OrionBanque.Forms
{
    public partial class Operation : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        Classe.Utilisateur uA;
        Classe.Operations O;
        private Int32 idC = 0;
        public bool cont = false;
        private bool PointageModif = false;

        public Operation(Int32 id, string mode, Classe.Utilisateur uP)
        {
            InitializeComponent();
            uA = uP;
            if (mode.Equals("INSERT"))
            {
                idC = id;
                kryptonHeader1.Values.Description = "Création";
                this.Text = "Création d'une Opération";
            }
            else if (mode.Equals("UPDATE"))
            {
                O = Classe.Operations.Charge(id);
                idC = O.IdCompte;
                kryptonHeader1.Values.Description = "Modification";
                this.Text = "Modification d'une Opération";
            }
            RemplisCb();
        }

        private void RemplisCb()
        {
            RemplisCategories();
            RemplisModePaiements();
            RemplisTiers();
            RemplisCheque();
            ChargeForm();
        }

        private void ChargeForm()
        {
            if (O != null)
            {
                try
                {
                    txtDateMvt.Text = O.Date.Day.ToString("00") + "/" + O.Date.Month.ToString("00") + "/" + O.Date.Year.ToString("0000");
                    txtCategorie.SelectedValue = O.IdCategorie;
                    txtLibelle.Text = O.Libelle;
                    txtModePaiement.SelectedValue = O.IdModePaiement;
                    txtMontant.Value = new Decimal(O.Montant);
                    if (O.DatePointage != null)
                        txtPointage.Checked = true;
                    txtTiers.Text = O.Tiers;
                }
                catch (Exception ex)
                {
                    KryptonMessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void RemplisModePaiements()
        {
            try
            {
                List<Classe.ModePaiement> lmp = Classe.ModePaiement.ChargeTout();
                txtModePaiement.DisplayMember = "Libelle";
                txtModePaiement.ValueMember = "Id";
                txtModePaiement.DataSource = lmp;
            }
            catch (Exception ex)
            {
                KryptonMessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void RemplisCheque()
        {
            try
            {
                List<Classe.Chequier> lc;
                if (O != null && O.IdModePaiement == 8)
                    lc = Classe.Chequier.ChargeListChequeMajOpe(this.idC, this.O.Id);
                else
                    lc = Classe.Chequier.ChargeTout(this.idC);
                kCbListCheque.DisplayMember = "NumCheque";
                kCbListCheque.ValueMember = "Id";
                kCbListCheque.DataSource = lc;
            }
            catch (Exception ex)
            {
                KryptonMessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void RemplisCategories()
        {
            try
            {
                List<Classe.Categorie> lc = Classe.Categorie.ChargeToutIdent();
                txtCategorie.DisplayMember = "Libelle";
                txtCategorie.ValueMember = "Id";
                txtCategorie.DataSource = lc;
            }
            catch (Exception ex)
            {
                KryptonMessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void RemplisTiers()
        {
            try
            {
                List<String> ls = Classe.Operations.ChargeToutTiers(uA);
                txtTiers.DataSource = ls;
            }
            catch (Exception ex)
            {
                KryptonMessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool FormValide()
        {
            bool retour = true;
            string text = "";
            // Test Date Choisie
            if (txtDateMvt.Text.Trim('/').Trim().Trim('/').Trim().Equals(string.Empty))
            {
                retour = false;
                text += "Il faut préciser un date de mouvement." + Environment.NewLine;
            }
            // Test si chèque emis alors un cheque est choisi
            if ((Int32)txtModePaiement.SelectedValue == 8 && kCbListCheque.SelectedValue == null)
            {
                retour = false;
                text += "Il faut choisir un chèque." + Environment.NewLine;
            }

            if (!retour)
            {
                KryptonMessageBox.Show(text, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return retour;

        }

        private void OK_Click(object sender, EventArgs e)
        {
            if (FormValide())
            {
                if (O != null)
                {
                    try
                    {
                        O.Date = Convert.ToDateTime(txtDateMvt.Text);
                        O.IdCategorie = (Int32)txtCategorie.SelectedValue;
                        O.IdModePaiement = (Int32)txtModePaiement.SelectedValue;
                        O.Libelle = txtLibelle.Text;
                        O.Montant = Double.Parse(txtMontant.Value.ToString());
                        O.Tiers = txtTiers.Text;
                        if (txtPointage.Checked && PointageModif)
                            O.DatePointage = DateTime.Now;
                        if (!txtPointage.Checked)
                            O.DatePointage = null;

                        Classe.Operations.Maj(O);

                        // GESTION CHEQUE
                        Classe.Chequier.DesaffecteCheque(O.Id);
                        if (kCbListCheque.Enabled)
                        {
                            Classe.Chequier ch = Classe.Chequier.Charge((Int32)kCbListCheque.SelectedValue);
                            ch.IdOperation = O.Id;
                            Classe.Chequier.Maj(ch);
                        }

                        cont = true;
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        KryptonMessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    try
                    {
                        Classe.Operations o = new OrionBanque.Classe.Operations();
                        o.Date = Convert.ToDateTime(txtDateMvt.Text);
                        o.IdCategorie = (Int32)txtCategorie.SelectedValue;
                        o.IdCompte = idC;
                        o.IdModePaiement = (Int32)txtModePaiement.SelectedValue;
                        o.Libelle = txtLibelle.Text;
                        o.Montant = Double.Parse(txtMontant.Value.ToString());
                        o.Tiers = txtTiers.Text;
                        if (txtPointage.Checked && PointageModif)
                            o.DatePointage = DateTime.Now;
                        if (!txtPointage.Checked)
                            o.DatePointage = null;

                        Classe.Operations.Sauve(o);

                        // GESTION CHEQUE
                        if (kCbListCheque.Enabled)
                        {
                            Classe.Chequier ch = Classe.Chequier.Charge((Int32)kCbListCheque.SelectedValue);
                            ch.IdOperation = Classe.Sql.GetLastInsertId("operations");
                            Classe.Chequier.Maj(ch);
                        }

                        cont = true;
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void txtPointage_CheckedChanged(object sender, EventArgs e)
        {
            PointageModif = true;
        }

        private void txtModePaiement_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtModePaiement.SelectedValue.ToString() == "8")
            {
                kCbListCheque.Enabled = true;
            }
            else
                kCbListCheque.Enabled = false;
        }

        private void kChooseDate_MouseClick(object sender, MouseEventArgs e)
        {
            txtDateMvt.Text = kChooseDate.SelectionStart.Day.ToString("00") + "/" + kChooseDate.SelectionStart.Month.ToString("00") + "/" + kChooseDate.SelectionStart.Year.ToString("0000");
            kChooseDate.Visible = false;
        }

        private void buttonSpecAny1_Click(object sender, EventArgs e)
        {
            string s = txtDateMvt.Text.Trim('/').Trim().Trim('/').Trim();
            if (!s.Equals(string.Empty))
            {
                kChooseDate.SelectionStart = DateTime.Parse(txtDateMvt.Text);
                kChooseDate.SelectionEnd = DateTime.Parse(txtDateMvt.Text);
            }
            else
            {
                kChooseDate.SelectionStart = DateTime.Now;
                kChooseDate.SelectionEnd = DateTime.Now;
            }
            kChooseDate.Visible = true;
        }

        private void txtDateMvt_Leave(object sender, EventArgs e)
        {
            string s = txtDateMvt.Text.Trim('/').Trim().Trim('/').Trim();
            if (!s.Equals(string.Empty))
            {
                try
                {
                    Convert.ToDateTime(txtDateMvt.Text);
                }
                catch
                {
                    KryptonMessageBox.Show("La date rentrée n'est pas reconnue", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtDateMvt.Focus();
                }
            }
        }

        private void btnSpecGestMP_Click(object sender, EventArgs e)
        {
            Forms.ModePaiement fm = new ModePaiement();
            fm.ShowDialog();
            RemplisModePaiements();
        }

        private void btnSpecGestCat_Click(object sender, EventArgs e)
        {
            Forms.Categories fm = new Forms.Categories();
            fm.ShowDialog();
            RemplisCategories();
        }

        private void btnSpecAddChequier_Click(object sender, EventArgs e)
        {
            Forms.AjoutChequier fm = new Forms.AjoutChequier(this.idC);
            fm.ShowDialog();
            RemplisCheque();
        }
    }
}
